package com.xingchi.tornado.storage.core.controller;

import com.xingchi.tornado.basic.Result;
import com.xingchi.tornado.storage.core.model.dto.FileUserRelDTO;
import com.xingchi.tornado.storage.core.service.FileUserRelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户文件关联信息
 *
 * @author xingchi
 * @date 2025/3/16 22:15
 */
@Slf4j
@RestController
@Tag(name = "用户文件关联信息", description = "包含文件查询、校验等相关操作")
@RequestMapping("/user/file")
public class FileUserRelController {

    @Autowired
    private FileUserRelService fileUserRelService;

    @Operation(summary = "根据文件的sha256值查询文件是否存在", description = "根据文件的sha256值查询文件是否存在，大文件上传时提前判断文件是否存在")
    @GetMapping("/exists/{sha256}")
    public Result<Boolean> exists(@PathVariable("sha256") String sha256) {
        return Result.ok(fileUserRelService.exists(sha256));
    }

    @Operation(summary = "重新构建用户文件关联信息", description = "重新构建用户文件关联信息(分片上传时使用，不然没有时机构建这个关系)")
    @GetMapping("/rebuildOrGetUserFileRel/{sha256}")
    public Result<FileUserRelDTO> rebuildOrGetUserFileRel(@PathVariable("sha256") String sha256) {
        return Result.ok(fileUserRelService.rebuildOrGetUserFileRelDTO(sha256));
    }

}
